﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SeleniumList.cs" company="">
// </copyright>
// <summary>
//   The selenium list.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace UITest.Selenium.SeleniumControls.HtmlControls
{
    using System.Collections.Generic;
    using System.Linq;

    using OpenQA.Selenium;
    using OpenQA.Selenium.Support.UI;

    using UITest.Selenium.SeleniumContext;

    /// <summary>
    ///     The selenium list.
    /// </summary>
    public class SeleniumList : SeleniumWebObject
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SeleniumList"/> class.
        /// </summary>
        /// <param name="seleniumContext">
        /// The selenium context.
        /// </param>
        /// <param name="findBy">
        /// The find by.
        /// </param>
        public SeleniumList(SeleniumContext seleniumContext, By findBy)
            : base(seleniumContext, findBy)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SeleniumList"/> class.
        /// </summary>
        /// <param name="seleniumContext">
        /// The selenium context.
        /// </param>
        /// <param name="webElement">
        /// The web element.
        /// </param>
        public SeleniumList(SeleniumContext seleniumContext, IWebElement webElement)
            : base(seleniumContext, webElement)
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets or sets the selected item as string.
        /// </summary>
        public string SelectedItemAsString
        {
            get
            {
                this.WaitForControlReady();
                var control = new SelectElement(this.GetWebElement());
                return string.Join(",", control.AllSelectedOptions.Select(x => x.Text));
            }

            set
            {
                this.WaitForControlReady();
                var control = new SelectElement(this.GetWebElement());
                control.DeselectAll();
                string[] selectArray = value.Split(',');
                foreach (string s in selectArray)
                {
                    control.SelectByText(s);
                }
            }
        }

        /// <summary>
        ///     Gets or sets the selected items.
        /// </summary>
        public string[] SelectedItems
        {
            get
            {
                this.WaitForControlReady();
                var control = new SelectElement(this.GetWebElement());
                IList<IWebElement> collection = control.AllSelectedOptions;
                var array = new string[collection.Count];
                for (int i = 0; i < collection.Count; i++)
                {
                    array[i] = collection[i].Text;
                }

                return array;
            }

            set
            {
                this.WaitForControlReady();
                var control = new SelectElement(this.GetWebElement());
                control.DeselectAll();
                for (int i = 0; i < value.Length; i++)
                {
                    control.SelectByText(value[i]);
                }
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The deselect all.
        /// </summary>
        public void DeselectAll()
        {
            this.WaitForControlReady();
            this.WriteTestOutput("DeSelect All on");
            var control = new SelectElement(this.GetWebElement());
            control.DeselectAll();
        }

        /// <summary>
        /// The item exists.
        /// </summary>
        /// <param name="text">
        /// The text.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool ItemExists(string text)
        {
            var control = new SelectElement(this.GetWebElement());
            return control.Options.Select(x => x).Any(x => x.Text == text);
        }

        /// <summary>
        /// The select by index.
        /// </summary>
        /// <param name="index">
        /// The index.
        /// </param>
        public void SelectByIndex(int index)
        {
            this.WaitForControlReady();
            this.WriteTestOutput("Select " + index + "on");
            new SelectElement(this.GetWebElement()).SelectByIndex(index);
        }

        /// <summary>
        /// The select by text.
        /// </summary>
        /// <param name="text">
        /// The text.
        /// </param>
        public void SelectByText(string text)
        {
            this.WaitForControlReady();
            this.WriteTestOutput("Select " + text + "on");
            new SelectElement(this.GetWebElement()).SelectByText(text);
        }

        /// <summary>
        /// The select by value.
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        public void SelectByValue(string value)
        {
            this.WaitForControlReady();
            this.WriteTestOutput("Select " + value + "on");
            new SelectElement(this.GetWebElement()).SelectByValue(value);
        }

        #endregion
    }
}